package com.fangyi.yunmusic.service.es;

import com.fangyi.yunmusic.config.ResourceConfig;
import com.fangyi.yunmusic.pojo.Song;
import com.fangyi.yunmusic.pojo.es.SongDocument;
import com.fangyi.yunmusic.pojo.vo.SongVO;
import com.fangyi.yunmusic.repository.es.SongRepository;
import com.fangyi.yunmusic.service.SongService;
import com.fangyi.yunmusic.service.impl.SongServiceImpl;
import com.fangyi.yunmusic.utils.PagedResult;
import com.fangyi.yunmusic.utils.PojoUtils;
import org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.WeightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery;

@Service
public class SongSearchService {

    @Autowired
    private SongRepository songRepository;

    @Autowired
    private ResourceConfig resourceConfig;

    @Autowired
    private SongServiceImpl songService;

    public Page<SongDocument> querySong(Integer pageNum, Integer pageSize, String query) {

        // 设置查询条件
        FunctionScoreQueryBuilder.FilterFunctionBuilder[] functions = {
                new FunctionScoreQueryBuilder.FilterFunctionBuilder(matchPhraseQuery("name", query), new WeightBuilder().setWeight(90)), // 设置权重
                new FunctionScoreQueryBuilder.FilterFunctionBuilder(matchPhraseQuery("lyric", query), new WeightBuilder().setWeight(10)),
        };
        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(functions)
                .scoreMode(FiltersFunctionScoreQuery.ScoreMode.SUM)     // 设置权重分求和模式
                .setMinScore(10);   // 设置权重分最低分

        // 设置分页
        Pageable pageable = PageRequest.of(pageNum, pageSize);

        NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder().withPageable(pageable).withQuery(functionScoreQueryBuilder).build();

        return songRepository.search(nativeSearchQuery);
    }

    public PagedResult querySongVO(Integer pageNum, Integer pageSize, String query) {
        Page<SongDocument> songDocumentPage = querySong(pageNum-1, pageSize, query);
        List<SongDocument> songDocuments = songDocumentPage.getContent();
        List<Song> songs = songDocuments.stream().map(songDocument -> {
            Song song = new Song();
            PojoUtils.copyProperties(songDocument, song);
            return song;
        }).collect(Collectors.toList());
        List<String> songIds = songs.stream().map(Song::getId).collect(Collectors.toList());

        List<SongVO> songVOs = songService.convertTOSongVOs(songs, songIds);
        return new PagedResult(songVOs, pageNum, songDocumentPage.getTotalPages(), songDocumentPage.getTotalElements());
    }

}
